package com.changgou.seckill.controller;

import com.changgou.entity.Result;
import com.changgou.entity.SeckillStatus;
import com.changgou.entity.StatusCode;
import com.changgou.seckill.service.SeckillOrderService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "SeckillOrderController")
@RestController
@RequestMapping("/seckillOrder")
@CrossOrigin
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping(value = "/add1")
    public Result add1(String time, Long id) {

        redisTemplate.opsForValue().set("StockCount", "5");

        ExecutorService service = Executors.newFixedThreadPool(100);
        IntStream.rangeClosed(0,100).forEach(i->{
            String username = "张三" + i;
            service.execute(()->{
                seckillOrderService.add(time,id,username);
            });
        });

        service.shutdown();
        return new Result(true,StatusCode.OK,"结束");
    }

    @GetMapping(value = "/add")
    public Result add(String time, Long id,String username) {
        // String username = "lisi";

        seckillOrderService.add(time,id,username);

        return new Result(true,StatusCode.OK,"正在排队...");
    }

    @GetMapping(value = "/query")
    public Result queryStatus() {
        String username = "lisi";
        SeckillStatus seckillStatus = seckillOrderService.queryStatus(username);
        if (seckillStatus != null) {
            return  new Result(true,StatusCode.OK,"查询状态成功",seckillStatus);
        }
        return new Result(false,StatusCode.NOTFOUNDERROR,"抢单失败...");
    }


}
